/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Database;

import com.sun.rowset.CachedRowSetImpl;
import java.sql.*;
//import Common.Table;
import java.sql.Timestamp;
import javax.sql.rowset.CachedRowSet;
/**
 *
 * @author localadmin
 */
public class Database
{
    Connection _conn=null;
    String _database="jdbc:sqlite:F:\\GPTAS\\GPTAS.sqlite";
    public static Database _instance= null;

   private Database()
    {
        try
        {
            Driver d = new org.sqlite.JDBC();
            DriverManager.registerDriver(d);
           _conn = DriverManager.getConnection(_database);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

   public static Database GetInstance()
   {
       if(_instance==null)
       {
           _instance=new Database();
       }
       return _instance;
   }
    public void GetTables()
    {
        try
        {

        }
        catch(Exception e)
        {

        }
    }

    public void GetColumns()
    {
        try
        {

        }
        catch(Exception e)
        {

        }
    }

    public ResultSet GetAllocations()
    {
        ResultSet rs=null;
        CachedRowSet crs=null;
        //Table result=null;
        try
        {
            Statement stat=_conn.createStatement();
            rs = stat.executeQuery("select * from Allocation");
           crs=new CachedRowSetImpl();
           crs.populate(rs);
            return crs;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return null;
        }
        finally
        {
            try
            {
             rs.close();
             _conn.close();
            }
            catch(Exception e)
            {

            }
        }
    }

    public void GetConstraints()
    {
        try
        {

        }
        catch(Exception e)
        {

        }
    }

    public void GetStudents()
    {
        try
        {

        }
        catch(Exception e)
        {

        }
    }

    public void InsertAllocations(String name,int noofgroups)
    {
        try
        {
            String sql = "INSERT INTO Allocation (name,createdatetime,updatedatetime,numberofgroups) VALUES(?,?,?,?)";
            PreparedStatement pstmt = _conn.prepareStatement(sql);
            pstmt.setString(1,name);
            System.out.println(name);
            java.util.Date d=new java.util.Date();
            pstmt.setTimestamp(2,new Timestamp(d.getTime()));
            System.out.println(d.getTime());
            pstmt.setTimestamp(3,new Timestamp(d.getTime()));
            pstmt.setInt(4,noofgroups);

                      
            pstmt.executeUpdate();
        }
        catch(Exception e)
        {
        System.out.println(e.getMessage());
        }
        finally
        {
            try
            {
                _conn.close();
            }
            catch(Exception e)
            {

            }
        }
    }
}
